﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.System;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// Pour en savoir plus sur le modèle d'élément Page vierge, consultez la page http://go.microsoft.com/fwlink/?LinkId=234238

namespace BlipSansBlop
{
    /// <summary>
    /// Une page vide peut être utilisée seule ou constituer une page de destination au sein d'un frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public static UIElement Obstacles { get; set; }
        public static UIElement Echelles { get; set; }
        public static UIElement Bumpers { get; set; }
        public static Canvas Personnages { get; set; }
        public static Canvas Ennemis { get; set; }
        public static Canvas Vies { get; set; }
        public static Canvas Ammunitions { get; set; }
        public static Canvas Tirs { get; set; }

        public static Random rand = new Random();

        public static int Vitesse = 1;
        public static int Gravite = 8;

        public static DispatcherTimer MainTimer = new DispatcherTimer();
        public static DispatcherTimer SousTimer = new DispatcherTimer();

        public MainPage()
        {
            this.InitializeComponent();
            //Ajout des Handlers à la page (j'ai mis 4 heures à écrire/trouver ces 2 lignes ...)
            Window.Current.CoreWindow.KeyDown += Page_KeyDown;
            Window.Current.CoreWindow.KeyUp += Page_KeyUp;
            MainPage.Obstacles = this.Niveau1_Obstacles;
            MainPage.Echelles = this.Niveau1_Echelles;
            MainPage.Bumpers = this.Niveau1_Bumpers;
            MainPage.Ennemis = this.Niveau1_Ennemis;
            MainPage.Ammunitions = this.Munitions;
            MainPage.Personnages = this.Characters;
            MainPage.Vies = this.Coeur;
            MainPage.Tirs = this.Bullets;
            MainPage.MainTimer.Interval = TimeSpan.FromTicks(Vitesse);
            MainPage.MainTimer.Tick += MainTimerTick;
            MainPage.MainTimer.Start();
            MainPage.SousTimer.Interval = TimeSpan.FromMilliseconds(100);
            MainPage.SousTimer.Tick += SousTimerTick;
            MainPage.SousTimer.Start();
        }


        private void EndGame()
        {
            var rootFrame = new Frame();
            if (!rootFrame.Navigate(typeof(CreditsPage)))
            {
                throw new Exception("Failed to create GoBack page");
            }
            Window.Current.Content = rootFrame;
            Window.Current.Activate();
        }

        public void SousTimerTick(object sender, object e)
        {
            foreach (Ennemi Enmi in Ennemis.Children)
            {
                Enmi.SwitchImage();
            }
            foreach (Munitions Ammo in Ammunitions.Children)
            {
                Ammo.ChangeAspect();
            }
            foreach (CoeurVie CRV in Vies.Children)
            {
                CRV.ChangeAspect();
            }
        }
        public void MainTimerTick(object sender, object e)
        {
            Perso.Update();
            if (Ennemis.Children.Count() == 0)
            {
                this.Niveau1_Obstacles.Children.Remove(this.Porte);
            }
            if (((Canvas.GetTop(Perso.Instance) + Perso.Instance.Height) <= 0)) EndGame(); 
            Stats.Update();
            foreach (Tir Bullet in Tirs.Children)
            {
                Bullet.SwitchToEtat();
            }
            foreach (Munitions Ammo in Ammunitions.Children)
            {
                Ammo.Update();
            }
            foreach (CoeurVie CRV in Vies.Children)
            {
                CRV.Update();
            }
            foreach (Ennemi Enmi in Ennemis.Children)
            {
                Enmi.Update();
            }
        }

        /// <summary>
        /// Invoqué lorsque cette page est sur le point d'être affichée dans un frame.
        /// </summary>
        /// <param name="e">Données d'événement décrivant la manière dont l'utilisateur a accédé à cette page. La propriété Parameter
        /// est généralement utilisée pour configurer la page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            
        }

        public static double DegreeToRadian(double angle)
        {
            return Math.PI * angle / 180.0;
        }

        public static double RadianToDegree(double angle)
        {
            return (angle * (180.0 / Math.PI));
        }

        public static Point Aprox(Point point, int shake)
        { // Let's do the the PointerShake
            return new Point(MainPage.rand.Next(((int)point.X - shake), ((int)point.X + shake)), MainPage.rand.Next((int)point.Y - shake, (int)point.Y + shake));
        }

        public static int AproxValue(int value, int shake)
        { // Let's do the the PointerShake
            return MainPage.rand.Next((value - shake), (value + shake));
        }

        /// <summary>
        /// Gestion des actions à effectuer lors de l'appui de touches
        /// </summary>
        /// <param name="sender">Correspond à Window.Current.CoreWindow</param>
        /// <param name="e">Fournit les données pour l'évènement</param>
        private void Page_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.VirtualKey == VirtualKey.O)
            {
                BoutonA.OPressed = true;
            }
            if (e.VirtualKey == VirtualKey.K)
            {
                BoutonB.KPressed = true;
            }
            if (e.VirtualKey == VirtualKey.Z)
            {
                Joystick.ZPressed = true;
                Joystick.Move();
            }
            if (e.VirtualKey == VirtualKey.D)
            {
                Joystick.DPressed = true;
                Joystick.Move();
            }
            if (e.VirtualKey == VirtualKey.S)
            {
                Joystick.SPressed = true;
                Joystick.Move();
            }
            if (e.VirtualKey == VirtualKey.Q)
            {
                Joystick.QPressed = true;
                Joystick.Move();
            }
            DebugControl.Update();
        }
        /// <summary>
        /// Gestion des actions à effectuer lors du relachement de touches
        /// </summary>
        /// <param name="sender">Correspond à Window.Current.CoreWindow</param>
        /// <param name="e">Fournit les données pour l'évènement</param>
        private void Page_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.VirtualKey == VirtualKey.O)
            {
                BoutonA.OPressed = false;
            }
            if (e.VirtualKey == VirtualKey.K)
            {
                BoutonB.KPressed = false;
            }
            if (e.VirtualKey == VirtualKey.Z )
            {
                Joystick.ZPressed = false;
                Joystick.Move();
            }
            if (e.VirtualKey == VirtualKey.D)
            {
                Joystick.DPressed = false;
                Joystick.Move();
            }
            if (e.VirtualKey == VirtualKey.S)
            {
                Joystick.SPressed = false;
                Joystick.Move();
            }
            if (e.VirtualKey == VirtualKey.Q)
            {
                Joystick.QPressed = false;
                Joystick.Move();
            }
            DebugControl.Update();
        }
    }
}
